/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package millenium3dchess;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import millenium3dchess.Player.Error;

/**
 *
 * @author Kenneth
 */
public class GuiDisplay extends JFrame implements Display, MouseListener {

    private int activeLevel;
    private JPanel level1, level2, level3, previewPanel;
    private DisplayBoard active = new DisplayBoard();
    private Chessman tempMan;
    private static GameSquare highlightSquare;

    public GuiDisplay() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Millenium 3D Chess");
        setIconImage(new ImageIcon("images\\millennium3Dico.png").getImage());
        setLocation(10, 10);
        setLayout(new FlowLayout());
        activeLevel = 0;
        previewPanel = new JPanel();
        previewPanel.setLayout(new BoxLayout(previewPanel, BoxLayout.Y_AXIS));

        setVisible(true);
    }

    @Override
    public void display() {
        active.update(this);
        add(active);
        updatePreviewPanel();
        getContentPane().add(previewPanel);
        pack();
    }

    private void updatePreviewPanel() {
        GridBagConstraints c2 = new GridBagConstraints();
        JPanel tempPanel;
        getContentPane().removeAll();
        previewPanel.removeAll();
        level1 = new JPanel();
        level2 = new JPanel();
        level3 = new JPanel();
        for (int z = 0; z < 3; z++) {
            if (z == 0) {
                tempPanel = level1;
            } else if (z == 1) {
                tempPanel = level2;
            } else {
                tempPanel = level3;
            }
            tempPanel.setLayout(new GridBagLayout());
            c2 = new GridBagConstraints();
            tempPanel.setBorder(new TitledBorder("Level " + (z + 1)));
            for (int x = 0; x < 8; x++) {
                switch (x) {
                    case 0:
                        c2.gridy = 8;
                        break;
                    case 1:
                        c2.gridy = 7;
                        break;
                    case 2:
                        c2.gridy = 6;
                        break;
                    case 3:
                        c2.gridy = 5;
                        break;
                    case 4:
                        c2.gridy = 4;
                        break;
                    case 5:
                        c2.gridy = 3;
                        break;
                    case 6:
                        c2.gridy = 2;
                        break;
                    case 7:
                        c2.gridy = 1;
                        break;
                    default:
                        c2.gridy = -1;
                        break;
                }
                for (int y = 0; y < 8; y++) {
                    c2.gridx = y + 1;
                    tempPanel.add(Game.getBoard()[z][y][x].preview(), c2);
                }
            }
            tempPanel.addMouseListener(this);
            previewPanel.add(tempPanel);
        }
        getContentPane().add(active);
        getContentPane().add(previewPanel);
        getContentPane().repaint();
    }

    @Override
    public void display(GameSquare gameSquare) {
        JPanel panel = new JPanel();
        panel.add(gameSquare);
        add(panel);
        pack();
    }

    @Override
    public void displayPotentialMoves(GameSquare gameSquare) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void handlePlayerError(Error error) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void handleError(String error) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    //sets the level value to the actual level
    //returns the internal representation
    public int setActiveLevel(int level) {
        return --activeLevel;
    }

    public int getActiveLevel() {
        return activeLevel + 1;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == level1) {
            activeLevel = 0;
        } else if (e.getSource() == level2) {
            activeLevel = 1;
        } else if (e.getSource() == level3) {
            activeLevel = 2;
        } else if (e.getSource() instanceof GameSquare) {
            int state;
            GameSquare temp = GameSquare.class.cast(e.getSource());
            if (tempMan != null) {
                if (temp.occupied()) {
                    highlightSquare.highlight = false;
                    highlightSquare.repaint();
                    state = 1;
                } else {
                    temp.setChessman(tempMan);
                    highlightSquare.highlight = false;
                    highlightSquare.setChessman(null);
                    highlightSquare.repaint();
                    highlightSquare = null;
                    updatePreviewPanel();
                    state = 2;
                }
                tempMan = null;
            } else {
                if (temp.occupied()) {
                    state = 3;
                    tempMan = temp.getChessman();
                    temp.highlight = true;
                    temp.repaint();                    
                    highlightSquare = temp;
                } else {
                    state = 4;
                }
            }
            System.out.println("State " + state);
        }
        active.update(this);
        repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    private class DisplayBoard extends JPanel{

        private GridBagConstraints c;

        public DisplayBoard() {
            setLayout(new GridBagLayout());
            c = new GridBagConstraints();
            setDoubleBuffered(true);
        }

        public void update(MouseListener listener) {
            removeAll();
            for (int x = 0; x < 8; x++) {
                switch (x) {
                    case 0:
                        c.gridy = 8;
                        break;
                    case 1:
                        c.gridy = 7;
                        break;
                    case 2:
                        c.gridy = 6;
                        break;
                    case 3:
                        c.gridy = 5;
                        break;
                    case 4:
                        c.gridy = 4;
                        break;
                    case 5:
                        c.gridy = 3;
                        break;
                    case 6:
                        c.gridy = 2;
                        break;
                    case 7:
                        c.gridy = 1;
                        break;
                    default:
                        c.gridy = -1;
                        break;
                }
                for (int y = 0; y < 8; y++) {
                    c.gridx = y + 1;
                    Game.getBoard()[activeLevel][y][x].addMouseListener(listener);
                    add(Game.getBoard()[activeLevel][y][x], c);
                }
            }
            pack();
        }
    }
}
